package com.cloudminga.gui;

import com.globussoft.amazons3.MultithreadingUpload;
import com.globussoft.amazons3.UploadFile;
import com.globussoft.xml.CreatXMLFile;
import com.globussoft.xml.SettingsXml;
import java.io.File;
import java.util.HashMap;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
public class MainDialog extends javax.swing.JFrame {
    /** Creates new form Files_backup */
    // @SuppressWarnings("static-access")
    String projectName = "";
    private MainForm frmMain;
    private static MainDialog mainDialogInstance=null;
    private static String uploadFrom ;
    public static MainDialog getInstance()
    {
        if(mainDialogInstance==null)
            mainDialogInstance= new MainDialog();
        return mainDialogInstance;
    }
    public static void setUploadFrom(String str)
    {
        uploadFrom=str;
    }

    private MainDialog() {
        initComponents();
        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setLocationRelativeTo(this);
        setResizable(false);
        frmMain = new MainForm();
        projectName = SettingsXml.getProjectName();
        try {
            /* code to set the look and feel according to the native operating system */

            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            SwingUtilities.updateComponentTreeUI(this);

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public void checkconfiguration() {
        String source = System.getProperty("user.home");
        String dir = source + "\\" + projectName + "\\" + "settings.xml";
        File sourceFile = new File(dir);
        if (sourceFile.exists()) {
            Thread runner = new Thread() {

                @Override
                public void run() {
                    dispose();
                    frmMain.setVisible(true);
                    frmMain.setOpened(true);
                }
            };
            runner.start();
        } else {
            this.dispose();
            Config_1_login configlogin = new Config_1_login(new javax.swing.JFrame(), true);
            configlogin.setVisible(true);
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        panel_backup = new javax.swing.JPanel();
        label_min_info = new javax.swing.JLabel();
        but_start = new javax.swing.JButton();
        but_configure = new javax.swing.JButton();
        but_history = new javax.swing.JButton();
        but_restore = new javax.swing.JButton();
        but_support = new javax.swing.JButton();
        but_hide = new javax.swing.JButton();
        checkbox = new javax.swing.JCheckBox();
        UpDownProgressBar = new javax.swing.JProgressBar();
        ProgressLevel = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Pom Blast Files Backup");

        label_min_info.setFont(new java.awt.Font("Tahoma", 1, 14));
        label_min_info.setText("Files Backup information");

        but_start.setFont(new java.awt.Font("Tahoma", 1, 11));
        but_start.setText("Start Backup");
        but_start.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                but_startActionPerformed(evt);
            }
        });

        but_configure.setFont(new java.awt.Font("Tahoma", 1, 11));
        but_configure.setText("Configure");
        but_configure.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                but_configureMouseClicked(evt);
            }
        });

        but_history.setFont(new java.awt.Font("Tahoma", 1, 11));
        but_history.setText("History");

        but_restore.setFont(new java.awt.Font("Tahoma", 1, 11));
        but_restore.setText("Restore Files");

        but_support.setFont(new java.awt.Font("Tahoma", 1, 11));
        but_support.setText("Support");

        but_hide.setFont(new java.awt.Font("Tahoma", 1, 11));
        but_hide.setText("Hide");
        but_hide.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                but_hideActionPerformed(evt);
            }
        });

        checkbox.setText("Show this box after every sucessfull backup");

        UpDownProgressBar.setToolTipText("Progress Bar");
        UpDownProgressBar.setDoubleBuffered(true);
        UpDownProgressBar.setOpaque(true);
        UpDownProgressBar.setStringPainted(true);

        javax.swing.GroupLayout panel_backupLayout = new javax.swing.GroupLayout(panel_backup);
        panel_backup.setLayout(panel_backupLayout);
        panel_backupLayout.setHorizontalGroup(
            panel_backupLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panel_backupLayout.createSequentialGroup()
                .addGap(30, 30, 30)
                .addComponent(label_min_info, javax.swing.GroupLayout.PREFERRED_SIZE, 300, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(80, 80, 80)
                .addGroup(panel_backupLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(but_start)
                    .addComponent(but_configure, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)))
            .addGroup(panel_backupLayout.createSequentialGroup()
                .addGap(410, 410, 410)
                .addComponent(but_history, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGroup(panel_backupLayout.createSequentialGroup()
                .addGap(410, 410, 410)
                .addComponent(but_support, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGroup(panel_backupLayout.createSequentialGroup()
                .addGap(10, 10, 10)
                .addComponent(checkbox, javax.swing.GroupLayout.PREFERRED_SIZE, 250, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(150, 150, 150)
                .addComponent(but_hide, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGroup(panel_backupLayout.createSequentialGroup()
                .addGap(30, 30, 30)
                .addGroup(panel_backupLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(ProgressLevel, javax.swing.GroupLayout.DEFAULT_SIZE, 510, Short.MAX_VALUE)
                    .addGroup(panel_backupLayout.createSequentialGroup()
                        .addGap(380, 380, 380)
                        .addComponent(but_restore, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(20, 20, 20))
                    .addGroup(panel_backupLayout.createSequentialGroup()
                        .addComponent(UpDownProgressBar, javax.swing.GroupLayout.PREFERRED_SIZE, 369, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())))
        );
        panel_backupLayout.setVerticalGroup(
            panel_backupLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panel_backupLayout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addGroup(panel_backupLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(panel_backupLayout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(label_min_info, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(panel_backupLayout.createSequentialGroup()
                        .addComponent(but_start)
                        .addGap(17, 17, 17)
                        .addComponent(but_configure)))
                .addGap(17, 17, 17)
                .addComponent(but_history)
                .addGap(17, 17, 17)
                .addComponent(but_restore)
                .addGap(26, 26, 26)
                .addComponent(but_support)
                .addGap(43, 43, 43)
                .addComponent(ProgressLevel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(UpDownProgressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(35, 35, 35)
                .addGroup(panel_backupLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(panel_backupLayout.createSequentialGroup()
                        .addGap(20, 20, 20)
                        .addComponent(checkbox))
                    .addComponent(but_hide, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(53, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(panel_backup, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(panel_backup, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void but_configureMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_but_configureMouseClicked
        // TODO add your handling code here:
        if (!MainForm.isFormOpened()) {
            checkconfiguration();
        } else {
            frmMain.resize();
        }

    }//GEN-LAST:event_but_configureMouseClicked

    private void but_hideMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_but_hideMouseClicked
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_but_hideMouseClicked

    private void but_startActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_but_startActionPerformed

        // Create New Seprate Thread for Store File and Folder On The Server
        if (but_start.getText().equals("Start Backup")) {
            but_start.setText("Stop Backup");
            new Thread() {
                @Override
                public void run() {
                    try {
                        SettingsXml settingXml = new SettingsXml();
                        String userName = SettingsXml.getUser();
                        CreatXMLFile xmlFileObj = new CreatXMLFile();
                        //  UploadFile upload = new UploadFile();
                        if (uploadFrom.contains("selectedFiles")) {
                            Thread t = new Thread() {
                                @Override
                                public void run() {
                                    try {
                                        MultithreadingUpload multiThreadingObj = new MultithreadingUpload("Upload", System.getProperty("user.home") + "/" + projectName + "/FilesToUpload.xml", "Manual");
                                        HashMap fileAndFolderToStore = CreatXMLFile.ReadXmlFile(System.getProperty("user.home") + "/" + projectName + "/FilesToUpload.xml",true);
                                        if (fileAndFolderToStore.size() > 0) {
                                            multiThreadingObj.uploadFile(fileAndFolderToStore, 5);
                                        }

                                    } catch (Exception ex) {
                                        ex.printStackTrace();
                                    }
                                }
                            };
                            t.start();
                            t.join();
                            System.out.println("File To Upload Is Finished");
                        } 
                        if (uploadFrom.contains("backupSet"))
                        {
                            Thread t1 = new Thread() {

                                @Override
                                public void run() {
                                    try {
                                        MultithreadingUpload multiThreadingObj = new MultithreadingUpload("Upload", System.getProperty("user.home") + "/" + projectName + "/BackUpSet.xml", "Manual");
                                        HashMap fileAndFolderToStore = CreatXMLFile.ReadXmlFile(System.getProperty("user.home") + "/" + projectName + "/BackUpSet.xml",true);
                                        if (fileAndFolderToStore.size() > 0) {
                                            multiThreadingObj.uploadFile(fileAndFolderToStore, 5);
                                        }
                                        //  upload.uploadFiles(System.getProperty("user.home") + "/"+projectName+"/BackUpSet.xml", fileAndFolderToStore, "Manual");
                                    } catch (Exception ex) {
                                        ex.printStackTrace();
                                    }
                                }
                            };
                            t1.start();
                            t1.join();
                            System.out.println("BackupSet Upload Is Finished");

                        }

                    } catch (Exception ex) {
                    }
                }
            }.start();
        } else {
            but_start.setText("Start Backup");
            UploadFile.setUploadContinue(false);
            but_start.setEnabled(false);
        }



    }//GEN-LAST:event_but_startActionPerformed

    private void but_hideActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_but_hideActionPerformed
        // TODO add your handling code here:
        this.setVisible(false);
    }//GEN-LAST:event_but_hideActionPerformed
    /**
     * @param args the command line arguments
     */
    /*    public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {
    public void run() {
    new MainDialog().setVisible(true);
    MainDialog frmMainDialog = new MainDialog();


    }
    });
    }*/
    // Variables declaration - do not modify//GEN-BEGIN:variables
    public static javax.swing.JLabel ProgressLevel;
    public static javax.swing.JProgressBar UpDownProgressBar;
    private javax.swing.JButton but_configure;
    private javax.swing.JButton but_hide;
    private javax.swing.JButton but_history;
    private javax.swing.JButton but_restore;
    public static javax.swing.JButton but_start;
    private javax.swing.JButton but_support;
    private javax.swing.JCheckBox checkbox;
    private javax.swing.JLabel label_min_info;
    private javax.swing.JPanel panel_backup;
    // End of variables declaration//GEN-END:variables
}
